let express = require("express")()
const { response, query } = require("express");
let expressStatic = require("express-static");
const { request } = require("http");
const port = 888

var mysql = require('mysql');
var sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'test',
    timezone: '08:00'
});

sql.connect()

// 从数据库中拿数据到页面上
express.get("/getgoods", (request, response) => {
        sql.query(`SELECT id,numbercolor,englishname,chinesename,price,img FROM goods`, (error, data) => {
            if (error) {
                console.log(error)
                response.send("error")
            } else {
                response.send(data)
            }
        })
    })
    //商品详情
express.get("/getcontent", (request, response) => {
        let id = request.query.id
        sql.query(`select * from goods where id=${id}`, (error, data) => {
            if (error) {
                console.log(error)
                response.send("error")
            } else {
                response.send(data[0])
            }
        })
    })
    //注册界面
express.get("/adduser", (request, response) => {
        let p = request.query
        sql.query(`insert into users (username,passnumber,password,phonenumber) values ("${p.name}","${p.passnumber}","${p.password}","${p.phonenumber}" )`, (error, data) => {
            if (error) {
                console.log(error)
                response.send("error")
            } else {
                response.send("success")
                    // console.log(data)
            }
        })
    })
    //登录界面
    //用户名登录
express.get("/searchuser1", (request, response) => {
    let p = request.query
    sql.query(`select * from users where username="${p.name}" and password="${p.password}"`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("3")
        } else {
            if (data.length == 0) {
                response.send("2")
            } else {
                response.send(JSON.stringify(data))
            }
            console.log(data)
        }
    })
})

//手机验证码登录
express.get("/searchuser2", (request, response) => {
    let p = request.query
    sql.query(`select * from users where phonenumber="${p.phonenumber}"`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("3")
        } else {
            if (data.length == 0) {
                response.send("2")
            } else {
                response.send(data)
            }
            console.log(data)
        }
    })
})

// 获取购买商品的数量和尺寸
// express.get("/insertgoods", (request, response) => {
//         let p = request.query
//         sql.query(`insert into orders (size,count,goodsid,userid) values ("${p.size}","${p.count}","${p.goodsid}","${p.userid}" )`, (error, data) => {
//             if (error) {
//                 console.log(error)
//                 response.send("error")
//             } else {
//                 response.send("success")
//             }
//         })
//     })
express.get("/insertbuyer", (request, response) => {
        let p = request.query
        sql.query(`insert into buyer (size,count,goodsid,userid,chinesename,englishname,color,price,img) values ("${p.size}","${p.count}","${p.goodsid}","${p.userid}" ,"${p.chinesename}","${p.englishname}","${p.color}","${p.price}","${p.img}")`, (error, data) => {
            if (error) {
                console.log(error)
                response.send("error")
            } else {
                response.send("success")


            }
        })
    })
    //生成购物车订单
    // express.get("/goodsup", (request, response) => {
    //         let p = request.query
    //         sql.query(`select * from orders ,users,goods  where orders.userid=users.id and users.id =${p.userid} and goods.id=orders.goodsid and goods.id=${p.goodsid}`, (error, data) => {
    //             if (error) {
    //                 console.log(error)
    //                 response.send("error")
    //             } else {
    //                 if (data.length === 0) {
    //                     response.send("2")
    //                 } else {
    //                     response.send(data)
    //                 }
    //             }
    //         })
    //     })
    //生成购物车订单
express.get("/goodsup", (request, response) => {
    let p = request.query
    sql.query(`select * from buyer where userid=${p.userid} `, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            if (data.length === 0) {
                response.send("2")
            } else {
                response.send(data)
            }
        }
    })
})


//删除订单
express.get("/deletelist", (request, response) => {
    let p = request.query
    sql.query(`delete from buyer where mid=${p.mid}`, (error, data) => {
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send("success")
        }
    })
})


express.use(expressStatic(__dirname + '/static'))
express.listen(port)